package lium.lbs.lbsim.adapter

import android.widget.ImageView
import android.widget.TextView
import com.abbott.mutiimgloader.util.JImageLoader
import com.abbott.mutiimgloader.weixin.WeixinMerge
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.buz.hjcdriver.bean.GroupModel
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lmlibrary.Constants
import com.qmuiteam.qmui.widget.QMUIRadiusImageView
import lium.lbs.lbsim.App
import lium.lbs.lbsim.R
import lium.lbs.lbsim.activity.MainActivity
import lium.lbs.lbsim.utils.PinnedHeaderItemDecoration

/**
 * quan adapter
 */
class GroupListAdapter
    (data: ArrayList<GroupModel>) : BaseMultiItemQuickAdapter<GroupModel, BaseViewHolder>(data),
    PinnedHeaderItemDecoration.PinnedHeaderAdapter {

    override fun isPinnedViewType(viewType: Int): Boolean {
        return viewType == GroupModel.ITEM_TYPE_SETION
    }

    var mainActivity: MainActivity? = null

    init {
        mainActivity = mContext as MainActivity?
        addItemType(GroupModel.ITEM_TYPE_DATA, R.layout.item_contact_list)
        addItemType(GroupModel.ITEM_TYPE_SETION, R.layout.item_list_setion)
    }

    override fun convert(helper: BaseViewHolder?, item: GroupModel?) {
        helper?.let {
            item?.let {
                when (helper.itemViewType) {
                    GroupModel.ITEM_TYPE_DATA -> {
                        initData(helper, item)
                    }
                    GroupModel.ITEM_TYPE_SETION -> {
                        helper.getView<TextView>(R.id.item_setion).text = item.letters + ""
                    }
                }
            }
        }
    }

    private fun initData(helper: BaseViewHolder, item: GroupModel) {
        helper.getView<TextView>(R.id.contacts_name).text = item.displayName + ""
        helper.getView<TextView>(R.id.contacts_code).text = item.gid + ""
        //getTelNumberActivity?.displayImage(item?.displayName,R.drawable.touxianggroup,helper.getView<QMUIRadiusImageView>(R.id.contacts_head))
       if (item?.portraitUri.size >1){
           try {
               var imageLoader = JImageLoader(App.getContext())
               imageLoader!!.displayImages(item?.portraitUri, helper?.getView<ImageView>(R.id.contacts_head), WeixinMerge())
           } catch (e: NoSuchFieldException) {
               e.printStackTrace()
           } catch (e: IllegalArgumentException) {
               e.printStackTrace()
           } catch (e: IllegalAccessException) {
               e.printStackTrace()
           }catch (e: IllegalStateException) {
               e.printStackTrace()
           }
       } else{
           val options = RequestOptions()
           options.placeholder(R.drawable.touxianggroup)
           Glide.with(App.getContext())
               .load(item?.portraitUri.get(0))
               .apply(options)
               .into(helper.getView<QMUIRadiusImageView>(R.id.contacts_head))
       }


    }


}